﻿using Future.Common.Const;
using Future.Common.Utils;
using Microsoft.AspNetCore.Authorization;
using System.IO;

namespace Future.Web.Controllers.SysModule
{
    /// <summary>
    /// 字典信息控制器
    /// </summary>
    public class DictionaryInfoController : AuthorizeController
    {
        private readonly IDictionaryInfoBLL _dictionaryInfoBLL;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="dictionaryInfoBLL"></param>
        public DictionaryInfoController(IDictionaryInfoBLL dictionaryInfoBLL)
        {
            _dictionaryInfoBLL = dictionaryInfoBLL;
        }

        /// <summary>
        /// 根据类型获取字典数据
        /// </summary>
        /// <param name="dictType"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("{dictType}")]
        [RequestCacheFilter(30)]
        public List<DictDataModel> GetListByType([FromRoute] string dictType)
        {
            List<DictDataModel> result = _dictionaryInfoBLL.GetDictDataByType(dictType);
            return result;
        }

        /// <summary>
        /// 动态条件分页查询
        /// </summary>
        /// <param name="dictTypeQueryDto"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        [HttpGet]
        [Permission("system:dict:query")]
        public PageModel<DictTypeModel> PageList([FromQuery] DictTypeQueryDto dictTypeQueryDto, [FromQuery] PageParModel page)
        {
            PageModel<DictTypeModel> result = _dictionaryInfoBLL.SelctPageList(dictTypeQueryDto, page);
            return result;
        }

        /// <summary>
        /// 创建字典类型数据
        /// </summary>
        /// <param name="dictTypeModel"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission("system:dict:add")]
        public bool AddDictType(DictTypeModel dictTypeModel)
        {
            bool result = _dictionaryInfoBLL.CreateDictType(dictTypeModel);
            return result;
        }

        /// <summary>
        /// 查询字典类型详细
        /// </summary>
        /// <param name="dictId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("{dictId}")]
        public DictTypeModel GetDictTypeById(Guid dictId)
        {
            return _dictionaryInfoBLL.GetDictTypeById(dictId);
        }

        /// <summary>
        /// 修改字典类型
        /// </summary>
        /// <param name="dictTypeModel"></param>
        /// <returns></returns>
        [HttpPut]
        [Permission("system:dict:edit")]
        public bool UpdateDictType(DictTypeModel dictTypeModel)
        {
            bool result = _dictionaryInfoBLL.UpdateDictType(dictTypeModel);
            return result;
        }

        /// <summary>
        /// 批量删除字典类型
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        [Permission("system:dict:remove")]
        public bool DeleteDictTypeList(List<Guid> ids)
        {
            return _dictionaryInfoBLL.DeleteDictTypeList(ids);
        }

        /// <summary>
        /// 导出excel
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public FileResult ExportExcel([FromQuery] DictTypeQueryDto dictTypeQueryDto, [FromQuery] PageParModel page)
        {
            page.PageNum = 1;
            page.PageSize = int.MaxValue;
            PageModel<DictTypeModel> result = _dictionaryInfoBLL.SelctPageList(dictTypeQueryDto, page);
            Stream stream = ExcelUtil.ExportExcel(result.Data);
            string fileName = string.Format("{0}.xls", DateTime.Now.ToString(DateTimeFormatConst.yyyyMMddHHmmss));
            return File(stream, "application/vnd.ms-excel", fileName);
        }

        /// <summary>
        /// 获取字典选择框列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<DictTypeModel> GetDictTypeList()
        {
            return _dictionaryInfoBLL.GetDictTypeList();
        }

        /// <summary>
        /// 动态条件分页查询字典数据
        /// </summary>
        /// <param name="dictDataQueryDto"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        [HttpGet]
        public PageModel<DictDataModel> DictDataPageList([FromQuery] DictDataQueryDto dictDataQueryDto, [FromQuery] PageParModel page)
        {
            Task<PageModel<DictDataModel>> pageModel = _dictionaryInfoBLL.DictDataPageList(dictDataQueryDto, page);
            return pageModel.Result;
        }

        /// <summary>
        /// 新增字典数据
        /// </summary>
        /// <param name="dicData"></param>
        /// <returns></returns>
        [HttpPost]
        public bool AddDictData(DictDataModel dicData)
        {
            return _dictionaryInfoBLL.AddDictData(dicData);
        }

        /// <summary>
        /// 修改字典数据
        /// </summary>
        /// <param name="dicData"></param>
        /// <returns></returns>
        [HttpPut]
        public bool UpdateDictData(DictDataModel dicData)
        {
            return _dictionaryInfoBLL.UpdateDictData(dicData);
        }

        /// <summary>
        /// 删除字典数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public bool DeleteDictDataList(List<Guid> ids)
        {
            return _dictionaryInfoBLL.DeleteDictDataList(ids);
        }

        /// <summary>
        /// 查询字典数据详细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("{id}")]
        public DictDataModel GetDictDataById(Guid id)
        {
            return _dictionaryInfoBLL.GetDictDataById(id);
        }
    }
}
